#!user/bin/python3
# -*- coding:UTF-8 -*-

################################################################################
#
# Copyright (c) 2020 openEuler.org, Inc. All Rights Reserved
#
################################################################################
"""
Handle failed tasks

Authors: xiaojianghui
Date:    21/12/2020 11:01 AM
"""

import time
from dbConnecttion.MysqlConn import Mysql
from tabletask import crawltask


def cur_date():
    """
        current date
    Returns:createTime:string

    """
    create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    return create_time


def repeat_task():
    """
    Parse the artificial CVE table, crawl the CVE official
    website data, and store it in the database
    return None
    """
    print("CVE官网数据为空的CVE进行再次抓取")
    mysql = Mysql()
    sql = "SELECT cve_num,pack_name,cve_version " \
          "FROM cve_origin_excel WHERE score_type = %s " \
          "OR score_type IS NULL OR cve_desc IS NULL OR nvd_score IS NULL"
    val = ('',)
    result = mysql.getMany(sql, val)
    print("CVE官网数据为空的 数据：", result)
    for i in result:
        crawllist = crawltask.crawling("https://nvd.nist.gov/vuln/detail/" + i["cve_num"])
        print("更新CVE数据")
        sql = "update cve_origin_excel set nvd_score=%s, cve_level=%s, cve_desc=%s, " \
              "repair_time=%s, vector_value=%s, attack_vector=%s, access_vector=%s, " \
              "attack_complexity=%s, access_complexity=%s, privilege_required=%s, " \
              "user_interaction=%s, scope=%s, confidentiality=%s, integrity=%s, " \
              "availability=%s, authentication=%s, cve_status=%s, update_time=%s, " \
              "score_type=%s where cve_num=%s and pack_name = %s and cve_version = %s"
        val = (crawllist[0], crawllist[1], crawllist[2], crawllist[3],
               crawllist[4], crawllist[5], crawllist[6],
               crawllist[7], crawllist[8], crawllist[9], crawllist[10],
               crawllist[11], crawllist[12], crawllist[13],
               crawllist[14], crawllist[15], 1, cur_date(), crawllist[16],
               i["cve_num"], i["pack_name"], i["cve_version"])
        mysql.update(sql, val)
        mysql.dispose()
    mysql.close()


def get_published_date_task():
    """
    Get the release date of nvd
    return None
    """
    print("Get the release date of nvd")
    mysql = Mysql()
    sql = "SELECT cve_id, cve_num, repair_time " \
          "FROM cve_vuln_center WHERE repair_time = '' " \
          "OR repair_time IS NULL"
    result = mysql.getMany(sql)
    if result is not None and result[0] is not None:
        for i in result:
            crawllist = crawltask.crawling("https://nvd.nist.gov/vuln/detail/" + i["cve_num"])
            if crawllist is not None and len(crawllist) >= 4 and crawllist[3]:
                sql = "update cve_vuln_center set repair_time = %s where cve_id = %s"
                val = (crawllist[3], i["cve_id"])
                mysql.update(sql, val)
                mysql.dispose()
    mysql.close()
